package com.myh.gulimall.product.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.myh.gulimall.product.entity.ProductAttrValueEntity;
import com.myh.gulimall.product.service.ProductAttrValueService;
import com.myh.gulimall.product.vo.AttrInfoVo;
import com.myh.gulimall.product.vo.AttrVo;
import com.myh.gulimall.product.vo.UpdateAttrValueVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.myh.gulimall.product.entity.AttrEntity;
import com.myh.gulimall.product.service.AttrService;
import com.myh.common.utils.PageUtils;
import com.myh.common.utils.R;



/**
 * 商品属性
 *
 * @author myh
 * @email 2722407149@qq.com
 * @date 2024-01-01 14:39:39
 */
@RestController
@RequestMapping("product/attr")
public class AttrController {
    @Autowired
    private AttrService attrService;

    @Autowired
    private ProductAttrValueService productAttrValueService;

    /*
    * 获取属性
    * */
    @GetMapping("/{attrType}/list/{catelogId}")
    public R baseList(@RequestParam Map<String, Object> params, @PathVariable("catelogId") Long catelogId, @PathVariable("attrType") String attrType){
        PageUtils page = attrService.queryPage(params, catelogId, attrType);
        return R.ok().put("page" , page);
    }

    /**
     * 列表
     */
//    @RequestMapping("/list")
//    //@RequiresPermissions("product:attr:list")
//    public R list(@RequestParam Map<String, Object> params){
//        PageUtils page = attrService.queryPage(params, catelogId);
//
//        return R.ok().put("page", page);
//    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrId}")
    //@RequiresPermissions("product:attr:info")
    public R info(@PathVariable("attrId") Long attrId){
        AttrInfoVo attr = attrService.getAttrInfoById(attrId);

        return R.ok().put("attr", attr);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("product:attr:save")
    public R save(@RequestBody AttrVo attr){
        attrService.saveAttr(attr);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("product:attr:update")
    public R update(@RequestBody AttrInfoVo attr){
        attrService.updateAttrById(attr);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("product:attr:delete")
    public R delete(@RequestBody Long[] attrIds){
        attrService.removeByIds(Arrays.asList(attrIds));

        return R.ok();
    }

    @GetMapping("/base/listforspu/{spuId}")
    public R listforspu(@PathVariable("spuId") Long spuId){
        List<ProductAttrValueEntity> productAttrValueEntities = productAttrValueService.listforspu(spuId);
        return R.ok().put("dara", productAttrValueEntities);
    }

    @PostMapping("/update/{spuId}")
    public R updateAttrValue(@PathVariable("spuId") Long spuId, @RequestBody List<UpdateAttrValueVo> updateAttrValueVos){
        productAttrValueService.updateAttrValue(spuId, updateAttrValueVos);
        return R.ok();
    }

}
